//package model.market;
//
//import java.util.ArrayList;
//import java.util.Iterator;
//import java.util.List;
//
//public class DealerEngine implements MarketEngine{
//
//	List<Trade> executedBuys=new ArrayList<Trade>();
//	List<Trade> executedSells=new ArrayList<Trade>();
//	DayMarketInfo[] historicalData;
//	
//	public PeriodResults executeOrders(
//							List<Trade> buyOrders, 
//							List<Trade> sellOrders, 
//							DayMarketInfo[] historicalData, 
//							double marketPrice) {
//		//input.. trade volumes
//		
//		//old market price
//		this.historicalData=historicalData;
//		
//		//auction till we run out of buyers or sellers..
//		
//		
//		marketPrice = reconcileAllPossible(buyOrders, sellOrders, marketPrice);
//		
//		//AM: now we tidy up the loose ends..
//		marketPrice =sortTheRest2(marketPrice, buyOrders, sellOrders);
//		
//		
//		
//		PeriodResults results = new PeriodResults();
//		results.setMarketPrice(marketPrice);
//		results.setExecutedBuys(executedBuys);
//		results.setExecutedSells(executedSells);
//		return results;
//		
//	}
//
//	
//	//AMXX: this is ripped from auction engine..
//	private double reconcileAllPossible(List<Trade> buyOrders, List<Trade> sellOrders, double marketPrice) {
//		Iterator<Trade> sells = sellOrders.iterator();
//		
//		Trade sell=null;
//		if(sells.hasNext()){
//			 sell = sells.next();
//		}else{
//			//Blow up??
//			//throw new MarketException("Nobody is selling!!");
//			return marketPrice;
//			
//			
//		}
//		int quantityToSell = sell.getQuantity();
//		
//		int quantityToBuy = 0;
//		
//		Iterator<Trade> buys = buyOrders.iterator();
//		
//		outer:while ( buys.hasNext()) {
//			Trade buy = (Trade) buys.next();
//			quantityToBuy = buy.getQuantity();
//			while(!buy.isComplete()){
//				if(quantityToSell==0){
//					//need the next sell order..
//					if(sells.hasNext()){
//						 sell = sells.next();
//						 quantityToSell = sell.getQuantity();
//					}else{
//						//Blow up??
//						//throw new MarketException("Nobody is selling!!");
//						return marketPrice;
//					}
//				}
//				int executionNumber =quantityToBuy;
//				if(quantityToBuy>quantityToSell){
//					executionNumber= quantityToSell;
//				}
//				quantityToBuy-=executionNumber;
//				quantityToSell-=executionNumber;
//				
//				if(buy.getStrategy()==Trade.AGGRESSIVE){
//					if(sell.getStrategy()!=Trade.AGGRESSIVE){
//						if(buy.getLimitPrice()==0||
//							 buy.getLimitPrice()>=sell.getPrice()){
//							//sell has set price..
//							marketPrice=sell.getPrice();
//						}else{
//							break outer;
//						}
//					}else{
//						//both aggressive, do their limits cross?..
//						if(buy.getLimitPrice()==0&&sell.getLimitPrice()==0){
//							//we have a simple deal!!
//						}else if(buy.getLimitPrice()==0){
//							marketPrice=Math.max(marketPrice, sell.getLimitPrice());
//							
//						}else if(sell.getLimitPrice()==0){
//							marketPrice=Math.min(marketPrice, buy.getLimitPrice());
//							
//						}else if(sell.getLimitPrice()<=buy.getLimitPrice()){
//							if(sell.getLimitPrice()<=marketPrice&&
//									buy.getLimitPrice()>=marketPrice){
//								//simple deal..
//							}else if(sell.getLimitPrice()<=marketPrice){
//								//the buy is the problem..
//								marketPrice = buy.getLimitPrice();
//							}else{
//								//sell must be the problem..
//								marketPrice = sell.getLimitPrice();
//							}
//						}else{
//							//no deal..
//							break outer;
//						}
//					}
//				}else{
//					//Passive buy..
//					if(sell.getStrategy()==Trade.AGGRESSIVE){
//						if(sell.getLimitPrice()==0||sell.getLimitPrice()<=buy.getPrice()){	
//							//use the buy price..
//							marketPrice=buy.getPrice();
//						}else{
//							break outer;
//						}
//					}else{
//						//No deal!!
//						break outer;
//					}
//					
//				}
//				//AMXX: temp tempnew Execution(buy,sell,executionNumber,marketPrice);
//				if(!executedBuys.contains(buy)){
//					executedBuys.add(buy);
//				}
//				if(!executedSells.contains(sell)){
//					executedSells.add(sell);
//				}
//			}
//		}
//		
//		return marketPrice;
//	}
//
//	private double sortTheRest2(double marketPrice,
//								List<Trade> buyOrders, 
//								List<Trade> sellOrders ) {
//		
//		double newPrice=0;
//		
//		int balancedQuantity = 0;
//		int directionQuantity = 0;
//		//AMXX: sort the algorithm properly.
//		
//		List<Trade> toBeExecuted = new ArrayList<Trade>();
//		for(int buyCount=0;buyCount<buyOrders.size();buyCount++){
//			Trade buy = buyOrders.get(buyCount);
//			balancedQuantity += buy.getExecutedQuantity();
//			
//			if(buy.getStrategy()==Trade.AGGRESSIVE&&
//					buy.getExecutedQuantity()!=buy.getQuantity()){
//				//We have a winner..
//				toBeExecuted.add(buy);
//				directionQuantity+=buy.getQuantity();
//			}
//		}
//		
//		if(directionQuantity!=0){
//			//We have a winner, lets work out the trades..
//			newPrice = execute(marketPrice, balancedQuantity, directionQuantity, toBeExecuted,true);
//		}else{
//			//Sells outnumber...
//			
//			for(int sellCount=0;sellCount<sellOrders.size();sellCount++){
//				Trade sell = sellOrders.get(sellCount);
//				
//				if(sell.getStrategy()==Trade.AGGRESSIVE&&
//						sell.getExecutedQuantity()!=sell.getQuantity()){
//					//We have a winner..
//					toBeExecuted.add(sell);
//					directionQuantity+=sell.getQuantity();
//				}
//			}
//			newPrice = execute(marketPrice, balancedQuantity, directionQuantity, toBeExecuted,false);
//		}
//		
//		if(newPrice>0){
//			return newPrice;
//		}
//		return marketPrice;
//	}
//
//
////AMXX: differnt strategy.	private double execute(double marketPrice, 
////								int balancedQuantity, 
////								int directionQuantity, 
////								List<Trade> toBeExecuted,
////								boolean downOrUp) {
////		double newPrice;
////		double shift=0;
////		boolean newPriceExecuted=false;
////		if(balancedQuantity==0){
////			if(historicalData.length>0){
////				DayMarketInfo info = historicalData[historicalData.length-1];
////				
////				double totalWorth = info.getTotalWorth();
////				double tradeVol = directionQuantity*marketPrice;
////				
////				shift=tradeVol/totalWorth;
////				
////			}else{
////				shift=.05;
////			}
////		}else{
////			shift=(double)directionQuantity/(double)balancedQuantity/5;
////		}
////		
////		if(downOrUp){
////			newPrice= marketPrice *(1+shift);
////		}else{
////			newPrice= marketPrice /(1+shift);
////		}
////		
////		for(int i=0;i<toBeExecuted.size();i++){
////			Trade buyToExecute = toBeExecuted.get(i);
////			if(buyToExecute.getLimitPrice()!=0&&(
////					buyToExecute.getLimitPrice()<newPrice&&
////					downOrUp||
////					buyToExecute.getLimitPrice()>newPrice&&
////					!downOrUp)){
////				break;
////			}
////			new Execution(buyToExecute,null,
////							buyToExecute.getQuantity()-buyToExecute.getExecutedQuantity(),
////							newPrice);
////			newPriceExecuted=true;
////			if(!executedBuys.contains(buyToExecute)){
////				executedBuys.add(buyToExecute);
////			}
////		}
////		if(!newPriceExecuted){
////			return 0;
////		}
////		return newPrice;
////	}
//	
//	
//	private double execute(double marketPrice, 
//			int balancedQuantity,
//			int directionQuantity, List<Trade> toBeExecuted, boolean downOrUp) {
//
//		double newPrice = marketPrice;
//		if (historicalData.length > 0) {
//			DayMarketInfo info = historicalData[historicalData.length - 1];
//			double totalWorth = info.getTotalWorth();
//		
//			for (int i = 0; i < toBeExecuted.size(); i++) {
//				Trade buyToExecute = toBeExecuted.get(i);
//				double tradeValue = buyToExecute.getQuantity()*newPrice;
//				double multiple= (1+Math.pow(Math.sqrt(tradeValue),2)/totalWorth);
//				if(downOrUp){
//					newPrice*=multiple;
//				}else{
//					newPrice/=multiple;
//				}
//				if (buyToExecute.getLimitPrice() != 0
//						&& (buyToExecute.getLimitPrice() < newPrice && downOrUp || buyToExecute
//								.getLimitPrice() > newPrice
//								&& !downOrUp)) {
//					break;
//				}
////	AMXX: temp temp			new Execution(buyToExecute, null, buyToExecute.getQuantity()
////						- buyToExecute.getExecutedQuantity(), newPrice);
//				if (!executedBuys.contains(buyToExecute)) {
//					executedBuys.add(buyToExecute);
//				}
//			}
//		}
//		
//		return newPrice;
//	}
//	
//
//}
